﻿using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Web.Mvc;
using FortuneArterial.Site.Model.System;
using Newtonsoft.Json;
using Bll = FortuneArterial.Site.BusinessLogicLayer;

namespace FortuneArterial.Site.Manage.Controllers
{
    public class SystemController : Controller
    {
        public Bll.Database.Context context = new Bll.Database.Context();

        [HttpGet]
        public ActionResult Upgrade()
        {
            return PartialView();
        }

        [HttpPost]
        public ActionResult Upgrade(List<object>  packages)
        {
            var message = string.Empty;

            Process msbuild = new Process();
            //设置进程信息
            msbuild.StartInfo.FileName = Path.GetFullPath(@"C:\Windows\Microsoft.NET\Framework64\v4.0.30319\MSBuild.exe");//TODO: %windir% 不生效？
            msbuild.StartInfo.Arguments = Server.MapPath("~/Site.csproj");
            //将cmd的标准输入和输出全部重定向到.NET的程序里
            msbuild.StartInfo.UseShellExecute = false; //此处必须为false否则引发异常
            msbuild.StartInfo.RedirectStandardInput = true; //标准输入
            msbuild.StartInfo.RedirectStandardOutput = true; //标准输出
            //不显示命令行窗口界面
            msbuild.StartInfo.CreateNoWindow = true;
            msbuild.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            //启动进程
            msbuild.Start();
            message = msbuild.StandardOutput.ReadToEnd();
            msbuild.WaitForExit();
            msbuild.Close();

            return Json(new {Success = true, Message = message});
        }

        public ActionResult ExportCodeRecordData()
        {
            string dataPath = Server.MapPath("~/temp/data/");
            if (!Directory.Exists(dataPath))
            {
                Directory.CreateDirectory(dataPath);
            }

            var datas = context.CodeRecordInfoSet;
            using (TextWriter tw = new StreamWriter(Path.Combine(dataPath, "CodeRecordInfo.json"), false))
            {
                foreach (var codeRecord in datas)
                {
                    tw.WriteLine(JsonConvert.SerializeObject(codeRecord));
                }
            }

            return Json(new { Success = true }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult ImportCodeRecordData()
        {
            string dataPath = Server.MapPath("~/temp/data/");
            using (TextReader tr = new StreamReader(Path.Combine(dataPath, "CodeRecordInfo.json")))
            {
                while (true)
                {
                    var str = tr.ReadLine();
                    if (string.IsNullOrEmpty(str))
                    {
                        break;
                    }

                    var codeRecord = JsonConvert.DeserializeObject<CodeRecordInfo>(str);
                    context.CodeRecordInfoSet.Add(codeRecord);
                }
            }
            context.SaveChanges();

            return Json(new { Success = true }, JsonRequestBehavior.AllowGet);
        }

    }
}
